package com.excel.read;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.read.biff.BlankCell;

public class HTMLTableConfig extends ExcelConfig {
	public String getHTMLForm() {

		return null;

	}

	public String tableConfig(String realPath) {
		File excel = null;
		
	
		Workbook w = null;
		try {
			excel = new File(realPath);
			w = Workbook.getWorkbook(excel);
			String names[] = w.getSheetNames();
			Sheet s = w.getSheet("表单");
			try {

				writeAllTable("", s);

			} catch (SameNameException e) {

			}
		} catch (Exception e) {

			System.out.println(e);
		} finally {
			w.close();
		}

		return "pmb";
	}

	public static List<Cell> findTables(Sheet s) {
		List<Cell> list = new ArrayList<Cell>();
		int curRowIndex = 1;
		int curColIndex = 0;
		int rows = s.getRows();
		int cols = s.getColumns();
		while (curRowIndex < s.getRows()) {
			Cell cell = s.getCell(1, curRowIndex);
			String content = cell.getContents();
			if (cell instanceof BlankCell) {
				content = "";
			}
			if (content != null && !content.trim().equals("")) {
				Cell aCell = s.getCell(1, curRowIndex - 1);
				content = aCell.getContents();
				if (aCell instanceof BlankCell) {
					content = "";
				}
				if (content == null || content.trim().equals("")) {
					list.add(cell);
				}
			}
			curRowIndex++;
		}
		return list;
	}



	public void writeAllTable(String filePath, Sheet s) throws BiffException,
			IOException, SameNameException {

		AllTable allTable = Config.allTable;
		List<Cell> list = findTables(s);
		Cell dateCell = s.getCell(0, 0);

		String aDate = dateCell.getContents();

		int maxPmbNo = (int) 1;

		for (int m = 0; m < list.size(); m++) {
			Cell aCell = list.get(m);
			FormTable table = new FormTable();
			int rowNum = getRowNum(s, aCell);

			int colNum = getColNum(s, aCell);
			table.rowIndex = aCell.getRow();
			table.colIndex = aCell.getColumn() - 1;
			table.cols = colNum;
			table.rows = rowNum;
			aCell = s.getCell(table.colIndex, table.rowIndex - 1);

			table.name = aCell.getContents();			
			table.pmbNo = maxPmbNo + m + 1;
			table.data = new String[table.rows][table.cols];
			int i = 0;
			int j = 0;
			for (i = 0; i < table.rows; i++) {
				for (j = 0; j < table.cols; j++) {
					Cell cell = s.getCell(j + table.colIndex, i
							+ table.rowIndex);
					String content = cell.getContents();
					table.data[i][j] = content;
				}
			}
			Cell cell = s.getCell(table.colIndex, i + table.rowIndex);
			String description = cell.getContents();
			table.setDescription(description);

			// 取得代码在哪一列
			int codeIndex = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("代码")) {
					codeIndex = k;
				}
			}
			table.codeIndex = codeIndex;
			// 取得字段名在哪一列
			int fieldNameIndex = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("字段名")) {
					fieldNameIndex = k;
				}
			}

			// 取得显示名在哪一列
			int displayNameIndex = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("名称")) {
					displayNameIndex = k;
				}
			}

			// 取得隐藏在哪一列
			int hiddenIndex = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("隐藏字段")) {
					hiddenIndex = k;
				}
			}
			
			int searchIndex = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("可查询")) {
					searchIndex = k;
				}
			}
			
			// 列宽
			int widthIndex = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("列宽")) {
					widthIndex = k;
				}
			}
			
			// vNameIndex
			int vNameIndex = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("变量名")) {
					vNameIndex = k;
				}
			}
			// vType
			int vType = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("控件类型")) {
					vType = k;
				}
			}
		
			// 驱动方式
			// vType
			int vDrive = -1;
			for (int k = 0; k < table.data[0].length; k++) {
				if (table.data[0][k].equals("驱动方式")) {
					vDrive = k;
				}
			}
			
			// 将codeIndex一列转为大写
			for (i = 1; i < table.rows; i++) {
				table.data[i][codeIndex] = table.data[i][codeIndex]
						.toUpperCase();

			}

			table.fieldIndex = fieldNameIndex;
			table.displayNameIndex = displayNameIndex;
			table.hiddenIndex = hiddenIndex;
			table.widthIndex = widthIndex;
			table.vNameIndex = vNameIndex;
			table.searchIndex = searchIndex;
			table.vTypeIndex = vType;
			table.vDriveIndex= vDrive;
			allTable.putForm(table.name, table);

		}

	}

	public static int getRowNum(Sheet s, Cell aCell) {
		int row = s.getRows();
		int i = 0;
		for (i = aCell.getRow(); i < s.getRows(); i++) {
			Cell cell = s.getCell(2, i);
			String content = cell.getContents();
			if (content == null || content.trim().equals("")) {
				row = i - aCell.getRow();
				break;
			}
		}
		if (i == s.getRows()) {
			row = i - aCell.getRow();
		}
		return row;
	}

	public  int getColNum(Sheet s, Cell aCell) {
		int columns = s.getColumns();
		for (int j = 1; j < s.getColumns(); j++) {
			Cell cell = s.getCell(j, aCell.getRow());
			String content = cell.getContents();
			if (content == null || content.trim().equals("")) {
				columns = j;
				break;
			}

		}
		return columns;
	}

	/**
	 * 构建查询树
	 * 
	 */
	public void getQueryTree() {
		
		return;
	}
	
	/**
	 * 根据查询树构建SQL
	 */

	public String getSQL() {
		
		return null;
	}
	
	
}
